What is run-sequence?
The run-sequence npm package is a tool for running a series of Gulp tasks in sequence. It is particularly useful for managing complex build processes where certain tasks need to be completed before others can start.
What are run-sequence's main functionalities?
Running tasks in sequence
This feature allows you to run Gulp tasks in a specific order. In this example, 'task1' will run first, followed by 'task2'.
const gulp = require('gulp');
const runSequence = require('run-sequence');
gulp.task('task1', function() {
// Task 1 code here
});
gulp.task('task2', function() {
// Task 2 code here
});
gulp.task('default', function(callback) {
runSequence('task1', 'task2', callback);
});
Running tasks in parallel
This feature allows you to run some tasks in parallel while ensuring that other tasks run in sequence. In this example, 'task1' will run first, followed by 'task2' and 'task3' running in parallel.
const gulp = require('gulp');
const runSequence = require('run-sequence');
gulp.task('task1', function() {
// Task 1 code here
});
gulp.task('task2', function() {
// Task 2 code here
});
gulp.task('task3', function() {
// Task 3 code here
});
gulp.task('default', function(callback) {
runSequence('task1', ['task2', 'task3'], callback);
});
Other packages similar to run-sequence
gulp
Gulp itself can handle task sequencing and parallel execution using its built-in methods. While run-sequence provides a more straightforward API for sequencing, Gulp's native methods offer more flexibility and are more actively maintained.
gulp-sequence
Similar to run-sequence, gulp-sequence allows you to run Gulp tasks in a specific order. However, it is less popular and less frequently updated compared to run-sequence.
undertaker
Undertaker is the task manager used internally by Gulp 4. It provides advanced task sequencing and parallel execution capabilities, making it a more modern and robust alternative to run-sequence.
run-sequence
Runs a sequence of gulp tasks in the specified order. This function is designed to solve the situation where you have defined run-order, but choose not to or cannot use dependencies.
Is your company hiring Node developers?
If you are hiring developers, you can support this project and future open source work by checking out our company, Qualified.io.
Qualified is a service for online skills-assessment that can help you easily vet developers across a wide range of real-world programming skills.
Please help support this project, and sign up for a free trial.
Each argument to run-sequence
is run in order. This works by listening to the task_stop
and task_err
events, and keeping track of which tasks have been completed. You can still run some of the tasks in parallel, by providing an array of task names for one or more of the arguments.
If the final argument is a function, it will be used as a callback after all the functions are either finished or an error has occurred.
Please Note
This was intended to be a temporary solution until the release of gulp 4.0 which should have support for defining task dependencies similarly.
Given that Gulp 4 appears to never be fully released, take that for what you will. Be aware that this solution is a hack, and may stop working with a future update to gulp.
Usage
First, install run-sequence
as a development dependency:
npm install --save-dev run-sequence
Then add use it in your gulpfile, like so (note these are only examples, please check the documentation for your functions for the correct way to use them):
var gulp = require('gulp');
var runSequence = require('run-sequence');
var del = require('del');
var fs = require('fs');
gulp.task('build', function(callback) {
runSequence('build-clean',
['build-scripts', 'build-styles'],
'build-html',
callback);
});
gulp.task('build-clean', function() {
return del([BUILD_DIRECTORY]);
});
gulp.task('build-scripts', function() {
return gulp.src(SCRIPTS_SRC).pipe(...)...
});
gulp.task('callback-example', function(callback) {
fs.readFile('...', function(err, file) {
console.log(file);
callback();
});
});
Using within gulp submodules
If you have a complex gulp setup with your tasks split up across different files, you may get the error that run-sequence
is unable to find your tasks. In this case, you can configure run-sequence
to look at the gulp within the submodule, like so:
var gulp = require('gulp'),
runSequence = require('run-sequence').use(gulp);
runSequence('subtask1', 'subtask2');
Options
There are a few global options you can configure on the runSequence
function.
Please note these are global to the module, and once set will affect every use of runSequence
.
Usage:
var runSequence = require('run-sequence');
runSequence.options.ignoreUndefinedTasks = true;
gulp.task('task', function(cb) {
runSequence('foo', null, 'bar');
})
showErrorStackTrace
: When set to false
, this suppresses the full stack trace from errors captured during a sequence.ignoreUndefinedTasks
: When set to true
, this enables you to pass falsey values in which will be stripped from the task set before validation and sequencing.
LICENSE
MIT License